package org.gruposp2p.dnie.ui.dialogs;

import dniegp2pclient.DNIeGP2PClientApp;
import java.io.File;
import javax.swing.JFileChooser;
import org.gruposp2p.dnie.utils.FileUtils;
import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;

/**
 *
 * @author jj
 */
public class DocumentSendedDialog extends javax.swing.JDialog {

    private String xmlSignedDocument;
    private ResourceMap resourceMap;

    /** Creates new form DocumentSendedDialog */
    public DocumentSendedDialog(
            java.awt.Frame parent, boolean modal, String xmlSignedDocument) {
        super(parent, modal);
        this.xmlSignedDocument = xmlSignedDocument;
        this.setLocationRelativeTo(parent);
        initComponents();
        resourceMap = Application.getInstance(DNIeGP2PClientApp.class)
                .getContext().getResourceMap(DocumentSendedDialog.class);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        messageLabel = new javax.swing.JLabel();
        closeButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(DocumentSendedDialog.class);
        messageLabel.setFont(resourceMap.getFont("messageLabel.font")); // NOI18N
        messageLabel.setIcon(resourceMap.getIcon("messageLabel.icon")); // NOI18N
        messageLabel.setText(resourceMap.getString("messageLabel.text")); // NOI18N
        messageLabel.setName("messageLabel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(DocumentSendedDialog.class, this);
        closeButton.setAction(actionMap.get("closeDialog")); // NOI18N
        closeButton.setIcon(resourceMap.getIcon("closeButton.icon")); // NOI18N
        closeButton.setText(resourceMap.getString("closeButton.text")); // NOI18N
        closeButton.setToolTipText(resourceMap.getString("closeButton.toolTipText")); // NOI18N
        closeButton.setName("closeButton"); // NOI18N

        saveButton.setAction(actionMap.get("saveCopy")); // NOI18N
        saveButton.setIcon(resourceMap.getIcon("saveButton.icon")); // NOI18N
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(closeButton))
                    .addComponent(messageLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(messageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(closeButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DocumentSendedDialog dialog = new DocumentSendedDialog(
                        new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void saveCopy() {
         JFileChooser chooser = new JFileChooser();
         chooser.showSaveDialog(
                 DNIeGP2PClientApp.getApplication().getView().getFrame());
         File file = chooser.getSelectedFile();
         if (file!= null) {
             FileUtils.save( xmlSignedDocument, file.getAbsolutePath(), "xml");
         }
         this.dispose();
    }

    @Action
    public void closeDialog() {
        this.dispose();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JLabel messageLabel;
    private javax.swing.JButton saveButton;
    // End of variables declaration//GEN-END:variables

}
